import baseconfig from "./baseconfig.js"
import APPUpdate from '@/common/appUpdate.js';
let Timer =null
function updateFn() {
  if(Timer != null){
		clearTimeout(Timer);
	}
	Timer = setTimeout(()=>{
		APPUpdate();
	}, 2500)
}
const httpClient = {
	request: function(method, url, data,json='',showLoading=true) {
		var loginToken = uni.getStorageSync('access_token');
		var headers={}
		if(json){
			headers = {
				"Authorization":loginToken,
				"Content-Type": "application/json"
			};
		}else{
			headers = {
				"Authorization":loginToken,
				"Content-Type": "application/x-www-form-urlencoded"
			};
		}
		return new Promise((resolve, reject) => {
			if(showLoading){
				uni.showLoading()
			}
			uni.request({
				url: url,
				header: headers,
				data: data,
				method: method,
				//dataType: 'json',
				success: function(res) {
					if(showLoading){
						uni.hideLoading()
					}
					if (res.data.code == 300) {
						uni.showToast({
							icon: 'none',
							duration: 1500,
							title: res.data.msg
						})
						setTimeout(function(){
							uni.redirectTo({url:'/pages/common/login'});
						},1000)
					}else if(res.data.code==500){
						uni.showToast({
							icon: 'none',
							duration: 1500,
							title: res.data.msg
						})
					} else {
						resolve(res.data);
					}
				},
				fail: function(err) {
					uni.hideLoading()
					reject(err)
				}
			})
		})
	},
	uplodRequest: function(url, data) {
		var loginToken = uni.getStorageSync('access_token');
		var headers = {
			"Authorization":loginToken
		};
		uni.showLoading()
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				header: headers,
				url: url,
				filePath: data.filePath,
				name: data.name,
				formData: {
					flag: data.flag
				},
				success: function(res) {
					uni.hideLoading()
					let ret=JSON.parse(res.data)
					if (ret.code == 300) {
						uni.showToast({
							icon: 'none',
							title: ret.msg
						});
						setTimeout(function(){
							uni.redirectTo({url:'/pages/home/login'});
						},1000)
						
					}else if (ret.code ==500) {
						uni.showToast({
							icon: 'none',
							duration: 1000,
							title: ret.msg
						});
					} else {
						resolve(ret);
					}
				},
				fail: function(err) {
					uni.hideLoading()
					reject(err)
				}
			})
		})
	},
	get: function(url, data) {
		// #ifdef APP-PLUS
		updateFn()
		//#endif
		let allurl = this.geturl(url);
		return this.request('GET', allurl, data)
	},
	post: function(url, data,showLoading=true) {
		// #ifdef APP-PLUS
		if(url!='system/agentApp/index/checkVer'){
			updateFn()
		}
		//#endif
		let allurl = this.geturl(url);
		return this.request('POST', allurl, data,'',showLoading)
	},
	upload: function(url, data) {
		// #ifdef APP-PLUS
		updateFn()
		//#endif
		let allurl = this.geturl(url);
		return this.uplodRequest(allurl, data)
	},
	
	postJson:function(url,data){
		let allurl = this.geturl(url);
		return this.request('POST', allurl, data,'json')
	},
	
	geturl: function(url) {
		// #ifdef H5
		let baseApiUrl = window.location.host
		if (baseApiUrl.indexOf('192.168') !== -1 || baseApiUrl.indexOf('localhost') !== -1) {
			baseApiUrl = '/agent/'
		} else {
			// baseApiUrl = baseconfig.server
			baseApiUrl = '/prod-api/'
		}
		// #endif
		// #ifndef H5
		var baseApiUrl = baseconfig.server
		// #endif
		return baseApiUrl + url
	}
};

export default httpClient
